package cn.sskxyz.security.authorization.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JacksonJsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    }

    public static String toJsonStr(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T fromJsonStr(String jsonStr, Class<T> tClass) {
        try {
            T result = objectMapper.readValue(jsonStr, tClass);
            return result;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T fromJsonStr(String jsonStr,ParameterHolder<T> holder) {
        try {
            return objectMapper.readValue(jsonStr, holder);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

